<template>
    <div class="min-h-screen py-8">
      <div class="container mx-auto px-4">
        <!-- 页面标题 -->
        <div class="mb-8">
          <h1 class="text-3xl md:text-4xl font-bold text-gray-800">
            购物车
          </h1>
          <p class="text-gray-500 mt-2">
            {{ cart.items.length }} 本图书
          </p>
        </div>
        
        <!-- 购物车为空 -->
        <div v-if="cart.items.length === 0" class="text-center py-16">
          <div class="w-20 h-20 mx-auto mb-4 text-gray-300">
            <i class="fa fa-shopping-cart text-5xl"></i>
          </div>
          <h3 class="text-lg font-medium text-gray-700 mb-2">你的购物车是空的</h3>
          <p class="text-gray-500 mb-6">添加一些喜欢的图书到购物车吧</p>
          <router-link to="/" class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-md transition-colors">
            <i class="fa fa-arrow-left mr-2"></i>
            继续购物
          </router-link>
        </div>
        
        <!-- 购物车有商品 -->
        <div v-else class="grid grid-cols-1 lg:grid-cols-12 gap-8">
          <!-- 购物车列表 -->
          <div class="lg:col-span-8">
            <div class="bg-white rounded-lg shadow-sm p-6 mb-6">
              <div class="flex justify-between items-center mb-6 pb-4 border-b">
                <h3 class="font-medium text-gray-900">商品</h3>
                <div class="grid grid-cols-3 gap-8 w-1/2">
                  <span class="font-medium text-gray-900 text-center">单价</span>
                  <span class="font-medium text-gray-900 text-center">数量</span>
                  <span class="font-medium text-gray-900 text-right">小计</span>
                </div>
              </div>
              
              <!-- 商品列表 -->
              <div class="space-y-6">
                <div v-for="item in cart.items" :key="item.id" class="flex items-center">
                  <!-- 商品图片 -->
                  <div class="w-20 h-28 flex-shrink-0 overflow-hidden rounded-md">
                    <img :src="item.cover" alt="Book cover" class="w-full h-full object-cover">
                  </div>
                  
                  <!-- 商品信息 -->
                  <div class="ml-4 flex-grow">
                    <h4 class="font-medium text-gray-900">{{ item.title }}</h4>
                    <p class="text-sm text-gray-500">{{ item.author }}</p>
                    <button @click="removeFromCart(item.id)" class="text-sm text-red-500 hover:text-red-700 mt-1 transition-colors">
                      移除
                    </button>
                  </div>
                  
                  <!-- 价格、数量、小计 -->
                  <div class="grid grid-cols-3 gap-8 w-1/2">
                    <span class="text-center text-gray-900">¥{{ item.price.toFixed(2) }}</span>
                    <div class="flex items-center justify-center">
                      <button @click="updateQuantity(item.id, item.quantity - 1)" :disabled="item.quantity <= 1" class="w-6 h-6 flex items-center justify-center text-gray-500 hover:text-gray-900">
                        <i class="fa fa-minus"></i>
                      </button>
                      <span class="mx-2 text-gray-900">{{ item.quantity }}</span>
                      <button @click="updateQuantity(item.id, item.quantity + 1)" class="w-6 h-6 flex items-center justify-center text-gray-500 hover:text-gray-900">
                        <i class="fa fa-plus"></i>
                      </button>
                    </div>
                    <span class="text-right text-gray-900 font-medium">¥{{ (item.price * item.quantity).toFixed(2) }}</span>
                  </div>
                </div>
              </div>
            </div>
            
            <div class="flex justify-between">
              <router-link to="/books" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                <i class="fa fa-arrow-left mr-2"></i>
                继续购物
              </router-link>
            </div>
          </div>
          
          <!-- 订单摘要 -->
          <div class="lg:col-span-4">
            <div class="bg-white rounded-lg shadow-sm p-6 sticky top-8">
              <h3 class="font-medium text-gray-900 mb-4">订单摘要</h3>
              
              <div class="space-y-3 mb-6">
                <div class="flex justify-between">
                  <span class="text-gray-600">商品总价</span>
                  <span class="text-gray-900">¥{{ cart.totalPrice.toFixed(2) }}</span>
                </div>
                <div class="flex justify-between">
                  <span class="text-gray-600">运费</span>
                  <span class="text-gray-900">¥0.00</span>
                </div>
                <div class="flex justify-between pt-3 border-t">
                  <span class="text-gray-900 font-medium">总计</span>
                  <span class="text-gray-900 font-medium">¥{{ cart.totalPrice.toFixed(2) }}</span>
                </div>
              </div>
              
              <button class="w-full py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-md transition-colors font-medium">
                结算
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { useCartStore } from '@/stores/cartStore.js';
  import { computed } from 'vue';
  
  // 获取购物车状态
  const cart = useCartStore();
  
  // 更新商品数量
  const updateQuantity = (id, quantity) => {
    if (quantity <= 0) {
      cart.removeFromCart(id);
    } else {
      cart.updateQuantity(id, quantity);
    }
  };
  
  // 从购物车移除商品
  const removeFromCart = (id) => {
    cart.removeFromCart(id);
  };
  </script>
  
  <style scoped>
  /* 可以添加自定义样式 */
  </style>
      